<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="具体活动事件" prop="type">
        <el-select
          v-model="queryParams.activityType"
          placeholder="请选择具体活动查询榜单"
          size="small"
          style="width: 220px"
        >
          <el-option
            v-for="option in typeOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="榜单类型" prop="type">
        <el-select
          v-model="queryParams.identity"
          placeholder="请勾选主播榜或者粉丝榜"
          size="small"
          style="width: 220px"
        >
          <el-option
            v-for="option in identities"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" size="mini" icon="el-icon-check" @click="repeatRank" plain>重新排名</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rankList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户uid" prop="uid" width="180" />
      <el-table-column label="排名" prop="voteRank" width="150" />
      <el-table-column label="第几次榜单" prop="count" width="120" />
      <el-table-column label="总票数" prop="voteNum" width="180" />
      <el-table-column label="基础票数" prop="baseVoteNum" width="120" />
      <el-table-column label="造假票数" prop="fakeNum" width="120" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:rank:config:edit']"
          >注水</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:rank:config:del']"
          >移除榜单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="fakeData" label-width="150px">
        <el-form-item label="需要添加的票数" prop="votes">
          <el-input v-model.number="fakeNum" placeholder="请输入造假的票数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getLabels } from "@/api/activity/rank/config";
import { listRank,toRedisRank,getActivityUsedInRank,removeRank,fakeVote } from "@/api/activity/rank/configSports";


export default {
  name: "topList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      rankList: [],
      // 第几次榜单
      countId: undefined,
      // 注水票数
      fakeNum:undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityType: undefined,
        uid:'',
        identity:undefined,
      },
      // 表单参数
      fakeData:{},
      typeOptions:[],
      identities:[
        {
          value: 1,
          label: "粉丝榜"
        },
        {
          value: 0,
          label: "主播榜"
        },
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    preGetLabels(){
      getLabels().then(response=>{
        this.typeOptions = response.data
      })
    },
    /** 查询条件列表 */
    getList() {
      this.preGetLabels();
      this.loading = true;
      if (this.queryParams.activityType !== undefined){
        listRank(this.queryParams).then(
          response => {
            this.rankList = response.records;
            this.total = response.total;
            this.countId = response.countId;
            this.loading = false;
          }
        );
      } else {
        this.total = 0;
        this.loading = false;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        uid: undefined,
        votes: undefined,
        activityType: undefined,
        userType: undefined
      };
      this.resetForm("formData");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.activityType === undefined || this.queryParams.identity === undefined){
        this.$message({
          message: '请同时选择具体活动类型和榜单类型',
          type: 'warning'
        });
        return
      }
      this.getList();
    },
    handleUpdate(row) {
      if (this.queryParams.activityType === undefined || this.queryParams.identity === undefined){
        this.$message({
          message: '请同时选择具体活动类型和榜单类型',
          type: 'warning'
        });
        return
      }
      let data = {"activityType":this.queryParams.activityType}
      getActivityUsedInRank(data).then(response=>{
        let newCount = response.data
        if (this.countId !== null && newCount > this.countId){
          this.$message({
            message: '只有活动的最新榜单排名才允许添加造假票数',
            type: 'warning'
          });
          return
        }

        this.reset();
        this.fakeData = {
          "uid": row.uid,
          "votes": row.fakeNum,
          "activityType": row.activityType,
          "userType": row.userType
        }

        this.open = true;
        this.title = "修改票数";
      })
    },
    handleDelete(row){
      const id = row.id || this.ids;
      const uid = row.uid;
      this.$confirm('是否确认移除uid为"' + uid + '"的榜单?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        let data = {
          "activityType": row.activityType,
          "deleteUid": uid,
          "userType": row.userType,
          "isAdmin":"youth"
        }
        return removeRank(data);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    submitForm:function(){
      this.fakeData["votes"] = this.fakeNum
      fakeVote(this.fakeData).then(response=>{
        this.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        activityType: undefined,
        uid:'',
        identity:undefined,
      },
      this.resetForm("queryForm");
      this.handleQuery();
    },
    repeatRank(){
      if (this.queryParams.activityType === undefined || this.queryParams.identity === undefined){
        this.$message({
          message: '请同时选择具体活动类型和榜单类型',
          type: 'warning'
        });
        return
      }

      if (this.countId === undefined || this.countId === null){
        this.$message({
          message: '不需要重新排名了',
          type: 'warning'
        });
        return;
      }

      let data = {
        "correct": false,
        "activityType":this.queryParams.activityType,
        "count": this.countId
      }
      toRedisRank(data);
    }
  }
};
</script>
